<?php namespace Controller;

    abstract class Controller {
        protected $BAD_REQUEST;
        protected $REQUEST;


        public function request() {
            header("Access-Control-Allow-Origin: *");
            header("Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS");
            header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");
            $json = file_get_contents('php://input');
            $this->REQUEST["method"] = $_SERVER["REQUEST_METHOD"];
            $request = [
                "method" => $this->REQUEST["method"],
                "json" => json_decode($json, true)
            ];
            return $request;
        }

        public function successJson ($response) {
            header("Access-Control-Allow-Origin: *");
            header("Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS");
            header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");
            header('Content-Type: application/json; charset=utf-8');
            header("HTTP/1.1 200 OK");
            $json = [
                "status" => 200,
                "techworks" => false,
                "response" => $response
            ];
            print_r(json_encode($json));
        }

        public function errorJsonDie($status, $data = null) {
            header("Access-Control-Allow-Origin: *");
            header("Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS");
            header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");
            header('Content-Type: application/json; charset=utf-8');
            $json = [
              "status" => $status,
              "techworks" => false,
                "response" => ($data !== null) ? $data : false
            ];
            header("HTTP/1.1 $status");
            print_r(json_encode($json));
            die();
        }
    }